package codetop.microsoft.T102;

import util.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 18362
 * @Create: 2022-08-15 20:53:58 星期一
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null)
            return result;
        Deque<TreeNode> dq = new LinkedList<>();
        dq.push(root);
        while (!dq.isEmpty()) {
            int size = dq.size();
            List<Integer> res = new ArrayList<>();
            for (int i=0; i<size; ++i) {
                TreeNode front = dq.poll();
                res.add(front.val);
                if (front.left != null)
                    dq.offer(front.left);
                if (front.right != null)
                    dq.offer(front.right);
            }
            result.add(res);
        }
        return result;
    }
}